﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using StarTrek.Ship;
using StarTrek.Weapon;
using StarTrek.World;

namespace StarTrek.UI.Console.MenuItem
{
    class MenuAttack:iMenuItem
    {
        private const string ACTION_KEY = "A";
        private readonly string INDENT = new string(' ', 8);
         public bool ProcessSomeRequest(MenuContext Menu, TextWriter sw, TextReader tr)
        {
            bool rVal = (Menu.Action == MenuAction);

            if (rVal)
            {
                int weaponChoice = 0;
                int vesselChoice = 0;
                float Enegery = 0;
                int index = 0;
                Galaxy theWorld = Galaxy.Instance;
                List<iWeapon> opt = theWorld.CurrentShip.GetWeapons().FindAll(
                    (w) => w.isFunctional && w.isOffensive);
               
                opt.ForEach( (w) => sw.WriteLine("{0}{1}. {2} {3}",INDENT ,index++,w.GetType().Name, w.Name));

                sw.Write(Properties.Resources.MenuAttackPromptWeapon + Properties.Resources.MenuPrompt);
                if (!(int.TryParse(tr.ReadLine(), out weaponChoice) 
                    && ((0 <= weaponChoice) && (weaponChoice < opt.Count))))
                    throw new InvalidDataException(Properties.Resources.MenuAttackErrorWeapon);

                if (opt[weaponChoice].NeedsEnegery)
                {
                    sw.Write(Properties.Resources.MenuAttackPromptEnegery + Properties.Resources.MenuPrompt);
                    if (!(Single.TryParse(tr.ReadLine(), out Enegery) && (0 <= Enegery)))
                        throw new InvalidDataException(Properties.Resources.MenuAttackErrorEnegery);
                }

                index = 0;
                List<iShip> Target = theWorld.GetLocalVessels(theWorld.CurrentShip.CurrentLocation.Parent);
                    Target.ForEach( (v)=> sw.WriteLine("{0}{1}. the {2}",INDENT ,index++,v.ShipName));

                sw.Write(Properties.Resources.MenuAttackPromptVessel + Properties.Resources.MenuPrompt);
                if (!(int.TryParse(tr.ReadLine(), out vesselChoice)
                    && (0 <= vesselChoice) && (vesselChoice<= Target.Count)))
                    throw new InvalidDataException(Properties.Resources.MenuAttackErrorVessel);


                ShipStatus result = theWorld.CurrentShip.Offense(Target[vesselChoice],opt[weaponChoice], Enegery);
                
                if (result.HullStrength <= 0)
                {
                    sw.WriteLine(Properties.Resources.MenuAttackResultFmtString1,
                              Target[vesselChoice].ShipName);
                }
                else
                {
                    sw.WriteLine(Properties.Resources.MenuAttackResultFmtString2,
                                 Target[vesselChoice].ShipName, result.ShieldStrength*100, result.HullStrength*100);
                }
            }
            return rVal;
        }

        public string MenuAction
        {
            get { return ACTION_KEY; }
        }

        public string Text
        {
            get { return Properties.Resources.MenuAttackText; }
        }

        public string VerboseText
        {
            get { return Properties.Resources.MenuAttackVerbose; }
        }

    }
}
